<?php
namespace App\Providers;

use App\User\Models\UserModel;
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
use Illuminate\Support\Facades\Request;
use Tymon\JWTAuth\JWTAuth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Exceptions\ApiException;

class JwtUserProvider extends EloquentUserProvider
{
    /**
     * @param array $credentials
     * @return UserModel|UserContract|null|void
     */
    public function retrieveByCredentials(array $credentials)
    {
        if (empty($credentials)) {
            return;
        }
        $userModel = new UserModel();
        $where = [];
        $where['user_name'] = $credentials['user_name'];
        $where['password'] = md5($credentials['password']);
        $where['status'] = 0;
        $userInfo = $userModel->alias('a')
            ->where($where)->selectRaw('a.*')->first();
        if (!empty($userInfo)) {
            $now = nowTime();
            $userInfo->last_login_time = $now;
            $userInfo->save();
            return $userInfo;
        } else {
            throw new ApiException(10001);
        }
    }

    public function validateCredentials(UserContract $user, array $credentials)
    {
        return true;
    }
}
